package com.c.ctgproject.ui.collect

import androidx.recyclerview.widget.LinearLayoutManager
import com.c.ctgproject.adapter.guang.CarHandlerDetailAdapter1
import com.c.ctgproject.base.BaseActivityV2
import com.c.ctgproject.databinding.ActivityCarHandlerDetailBinding
import com.c.ctgproject.entity.guang.CARHandlerDetailListEntity
import com.c.ctgproject.http.ApiMethods
import com.c.ctgproject.http.MyObserverHandlerLoading
import com.c.ctgproject.utils.*
import java.lang.StringBuilder

/**
 *作者:LiBW
 *创建日期: 2021/9/23
 *描述: 拆解单详情
 */
class CarHandlerDetailActivity : BaseActivityV2() {
    private val mBinding by lazy { ActivityCarHandlerDetailBinding.inflate(layoutInflater) }
    override fun getLayout() = mBinding.root
    private val mId by lazy { intent.getIntExtra(MyConstants.ID, -1) }
    private val mList = ArrayList<CARHandlerDetailListEntity.CarDismantleOrderBean>()
    private val mAdapter by lazy { CarHandlerDetailAdapter1(this, mList) }
    private var page = 1
    private var search = ""
    override fun event() {
        mBinding.mCarRV.adapter = mAdapter
        mBinding.mCarRV.layoutManager = LinearLayoutManager(this)

    }

    override fun click() {
//        mBinding.mSearchET.handlerText {
//            if (it.isEmpty() || it == "") {
//                search = ""
//                page = 1
//                getData()
//            }
//        }
//        mBinding.mSearchTV.setOnClickListener {
//            search = mBinding.mSearchET.text.handlerNull()
//            page = 1
//            getData()
//        }
//        mBinding.mRefresh.setOnLoadMoreListener {
//            page++
//            getData()
//        }
//
//        mBinding.mRefresh.setOnRefreshListener {
//            page = 1
//            getData()
//        }
    }

    override fun onResume() {
        super.onResume()
        page = 1
        getData()
    }

    private fun getData() {
//        ApiMethods.apiSubscribe(ApiMethods.retrofit.carDismantleList(search, page,mId), MyObserverHandlerLoading(this, loading) {
        ApiMethods.apiSubscribe(ApiMethods.retrofit.carDismantleList(mId), MyObserverHandlerLoading(this, loading) {
            MyLog.d("拆解单详情回调:$it")
            val data = getGson().fromJson(it, CARHandlerDetailListEntity::class.java).carDismantleOrder
            data?.forEach { bean ->
                bean.infoList?.forEach { info ->
                    val sb = StringBuilder()
                    info.partsList?.forEach { part ->
                        part.partsName?.let {
                            sb.append(part.partsName.handlerNull() + "x" + part.partsCount.handlerNull()+", ")
                        }
                    }
                    var str=sb.handlerNull()
                    if (str.isNotEmpty()){
                        str=str.substring(0,str.length-1)
                    }
                    info.myStr = str
                }

            }
            data?.let {
                mList.clear()
                mList.addAll(data)
            }
            mAdapter.notifyDataSetChanged()
        })
    }
}